<?php
namespace app\houtai\controller\server;

use app\houtai\controller\BaseController;
use app\houtai\controller\RedisHash;
use think\Db;
use think\Request;
use think\db\Expression;
use think\paginator\driver\Bootstrap;


class RedisController extends BaseController{
  
  
	/**
   *  @brief  : 服务器开服列表
   *  @return : page
   *  @details: 
   */

  public function indexAction() {
      $re = new RedisHash();
      $res = $re->keys("RunTime_Channel_43776*");//读取所有字段\
      $data = [];
      for ($i=0;$i<count($res);$i++){
          $str = explode(':',$res[$i]);
          $data[$i]['code'] = $str[1];
          $data[$i]['val'] = $re->hval($res[$i]);
      }
      $total = count($data);
      $path = Request::instance()->baseUrl();
      $page = new \org\dtcms($data, $total <= 0 ? 1 : $total, 1, $total, false, ['path' => $path]);
      $tree = $data;
      //$tree = list_to_tree($list, 'UserID', 'ParentId', '_child', 0);
      return $this->fetch('', [
           
          'page'  => $page,
          'Total' => $total,
          'tree'  => $tree,
      ]);
  }

  /**
   *  @brief  : 私人场开服列表
   *  @return : page
   *  @details: 
   */
  public function OnelistAction() {
   

    $re = new RedisHash();
    $res = $re->hval("channel_43776:type_16:service");
    $total= count($res);
    foreach ($res as $key => $v) {
        $res[$key]['nowtimer'] = date("Y-m-d H:i:s",$v['nowtimer']);
    };
    $path = Request::instance()->baseUrl();
    $page = new \org\dtcms($res, $total <= 0 ? 1 : $total, 1, $total, false, ['path' => $path]);
    $tree = $res;
    //$tree = list_to_tree($list, 'UserID', 'ParentId', '_child', 0);
    return $this->fetch('', [
           
            'page'  => $page,
            'Total' => $total,
            'tree'  => $tree,
        ]);
  }

  /**
   *  @brief  : 俱乐部开服房间
   *  @return : page
   *  @details: 
   */
  public function RoomAction() {
   

    $re = new RedisHash();
    $ress = $re->keys("Club:work:channel_43776_10613*");
    //获取redis对应key的value值（string）
    $taNumb = count($ress);
    $tables = [];
    for ($i=0; $i < $taNumb ; $i++) { 
      $tables[] = json_decode($re->get($ress[$i]),true);
    }
    
    $tabless = [];
    $userids = [];
    //根据groupid重新分组···合并
    foreach ($tables as $key => $v) {
        if (!isset($tabless[$v['groupid']])) {
            $tabless[$v['groupid']]['data'][] = $v;
            $tabless[$v['groupid']]['userid'] = $v['userid'];
            $tabless[$v['groupid']]['groupid'] = $v['groupid'];
        }else{
        $tabless[$v['groupid']]['data'][]=$v;
       }
       
    }
    $tabless = array_merge($tabless);
     //分页，看不懂自己研究吧，做出来我也看不懂
    $total = count($tabless);
    $size = 3;//每页显示的条数(根据亲友团标识)
    $ceil = ceil($total/$size); //总页数
    $yu = $total%$ceil; //最后一页剩余几条
    $pag = $this->request->param('page/d',1);//跳转的页数
    $page = $pag * $size; //每页展示的条数
    $p = $page > 0 ? $page : $size;//三元运算判断是否首页
    $aa = $pag -1; //为下面循环做准备，（跳转页数-1）* 展示条数=循环的起始位置
    $a = $aa * $size;  
    if ($pag >= $ceil) {
        $p = $total;
    }
    $ta = [];
    for ($i=$a; $i <$p ; $i++) { 
      $ta[] = $tabless[$i];
      $userids[] = $tabless[$i]['userid'];
    }
    $userids = array_unique($userids);
    $userids = array_values($userids);
    $where = []; 
    $where['UserID'] = array('in', $userids); 
    $listDb = table('CYBasicDB.dbo.AccountsInfo', 'rydb.basic')->where($where)->select();

   //循环插入群主个人信息
    // foreach ($tabless as $key => $v) {
    //      $tabless[$key]['num'] = count($v['data']);
    //      for ($i=0; $i <count($listDb) ; $i++) { 
    //           if ($listDb[$i]['UserID'] == $v['userid']) {
    //             $tabless[$key]['userid'] = $listDb[$i];
    //           }
    //      }       
    // };
    for ($i=0; $i <count($listDb) ; $i++) { 
      $ta[$i]['num'] = count($ta[$i]['data']);
      if ($listDb[$i]['UserID'] = $ta[$i]['userid']) {
         $ta[$i]['userid'] = $listDb[$i];
      }
    };
    $url = Request::instance()->baseUrl();
    $pages = new \org\dtcms($ta, $pag < 0 ? 1 : $size, $pag, $total, false, ['path' => $url]);
    return $this->fetch('', [
            'page'  => $pages,
            'TaNumb' => $taNumb,
        ]);
    }

    
}